package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.PinYinUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.CwZykm;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyBxdMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.CwZykmMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.CwZykmParam;
import cn.stylefeng.guns.sys.modular.system.service.CwZykmService;
import cn.stylefeng.roses.core.page.PageFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 摘要科目表 服务实现类
 * </p>
 *
 * @author gong
 * @since 2021-02-01
 */
@Service
public class CwZykmServiceImpl extends ServiceImpl<CwZykmMapper, CwZykm> implements CwZykmService {

    @Resource
    private BtyBxdMapper btyBxdMapper;

    @Override
    public void add(CwZykmParam param){
        CwZykm entity = getEntity(param);
        String pinInCode = PinYinUtil.getPinYinCode(entity.getName());
        entity.setZjm(pinInCode);
        this.save(entity);
    }

    @Override
    public void delete(CwZykmParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CwZykmParam param){
        CwZykm oldEntity = getOldEntity(param);
        CwZykm newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CwZykm findBySpec(CwZykmParam param){
        QueryWrapper<CwZykm> qw = new QueryWrapper<>();
        qw.eq("name",param.getName()).eq("bty_cw_kjkm_id",param.getBtyCwKjkmId());
        CwZykm cwZykm = this.baseMapper.selectOne(qw);
        return cwZykm;
    }

    @Override
    public List<CwZykm> findListBySpec(CwZykmParam param){
        QueryWrapper<CwZykm> qw = new QueryWrapper<>();
        qw.eq("bty_cw_kjkm_id",param.getBtyCwKjkmId());
        List<CwZykm> list = this.baseMapper.selectList(qw);
        return list;
    }

   /* @Override
    public PageResult<CwZykmResult> findPageBySpec(CwZykmParam param){
        Page pageContext = getPageContext();
        IPage<CwZykmResult> page = this.baseMapper.customPageList(pageContext, param);
        return null;//new PageResult<>(page);
    }*/

    @Override
    public LayuiPageInfo findPageBySpec(CwZykmParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CwZykmParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private CwZykm getOldEntity(CwZykmParam param) {
        return this.getById(getKey(param));
    }

    private CwZykm getEntity(CwZykmParam param) {
        CwZykm entity = new CwZykm();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public boolean queryBxds(CwZykmParam param){
        QueryWrapper<BtyBxd> bxdqw=new QueryWrapper<>();
        bxdqw.eq("cw_zykm_id", param.getId());
        List<BtyBxd> bxds = btyBxdMapper.selectList(bxdqw);
        if(bxds.size()>0){
            return true;
        }
        return false;
    }

}
